//index.js
//获取应用实例
var util = require('../../utils/util.js')
var app = getApp()
Page({
  data: {
    todayExpend: "10",
    monthExpend: "0",
    yearExpend: "0",
    todayRecord:[],
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  recodeExpend: function () {
    wx.navigateTo({
      url: '../../pages/record-expend/record-expend',
    })
  },
  //提交数据函数(POST方式)
  submitData:function(){

    wx.request({
      url: "http://www.52sggs.com/admin/jsonAdmin.ashx?action=insertKuaidi100",
      method: "POST",
      data: {
        ApiKey: 'dkdkskdjlskj9eikljvkfklkff',
        CustomerId: 'kdkkdkdkdkdkdkdk',
        Remark: '测测测度度工在在困顺',
        CompanyId: 'dskdkj292993939'
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res.data);
        //wx.navigateBack({
        //  delta: 1  //小程序关闭当前页面返回上一页面
        //})
        if(res.data.Success==true){
          wx.showToast({
            title: '成功！',
            icon: 'success',
            duration: 2000        
          })
        } else {
          wx.showToast({
            title: res.data.Message,
            icon: 'none',
            duration: 2000
          })
        }
      },
    })
  },
//今日账单item点击
onTodayBillItemClick:function(e){
  let index = e.currentTarget.dataset.index;
  
},
//今日账单item长按
ononTodayBillLongItemClick:function(e){

},

  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    var that = this;//=====注意此处，要用that 指代this=====
    wx.request({
      url: 'http://www.52sggs.com/admin/jsonAdmin.ashx?action=getCompany&CompanyId=74709097-0d4a-4db8-bb06-659323ae8624', //服务器地址
      method: 'get',// OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      data: {
      },
      header: {// 设置请求的 header
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res);
        that.setData({ //======不能直接写this.setDate======
          message: res.data, //在相应的wxml页面显示接收到的数据
        });
      }
    })

  },
  onShow: function () {
    let bill;
    const todayDate = util.formatTime(new Date(), "yyyy-MM-dd");
    try {
      bill = wx.getStorageSync('Bill');
    } catch (e) {
    }
    if (bill != "") {
      let todayMoney = 0;
      let monthMoney = 0;
      let yearMoney = 0;
      let todayRecord = [];
      for (let key of bill) {
        //同一天
        if (util.dateIsDifference(key.date, todayDate, "d")) {
          todayMoney += key.spendMoney;
          todayRecord.push(key);
        };
        //同一月
        if (util.dateIsDifference(key.date, todayDate, "n")) {
          monthMoney += key.spendMoney;
        };
        //同一月
        if (util.dateIsDifference(key.date, todayDate, "y")) {
          yearMoney += key.spendMoney;
        };
      }
      this.setData({
        todayExpend: todayMoney,
        monthExpend: monthMoney,
        yearExpend: yearMoney,
        todayRecord:todayRecord,
      });
    };

  },

 onShareAppMessage: function () {
    return {
      title: '账单',
      path: 'pages/index/index',
      success: function (res) {
        // 分享成功
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },
    getUserInfo: function(e) {
      console.log(e)
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
})
